package com.decent.manager.system.dao;

import com.decent.common.annotation.ReturnCheck;
import com.decent.common.entity.manager.SystemRole;
import com.decent.common.entity.manager.SystemUser;
import com.decent.manager.system.dto.SystemUserDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户角色sql管理
 *
 * @author wangyan
 **/
@Mapper
public interface SystemUserRoleDao {
    /**
     * 查询用户角色
     *
     * @param userId 用户
     * @return 结果
     */
    @Select("select t2.id,t2.role_name,t2.role_code from system_user_role t1 " +
            " inner join system_role t2 on t1.role_id = t2.id and t2.role_status = 'NORMAL' " +
            " where t1.user_id = #{userId} ")
    SystemRole queryRoles(Long userId);

    /**
     * 创建用户-角色关系
     *
     * @param user 新增数据
     * @return 新增行数
     */
    @SuppressWarnings("UnusedReturnValue")
    @Insert("insert into system_user_role " +
            "(user_id, role_id, creator, create_time, last_modifier, update_time) " +
            "values " +
            "(#{id}, #{roleId}, #{creator}, now(), #{creator}, now())")
    @ReturnCheck(info = "创建用户-角色关系失败")
    int createUserRole(SystemUserDTO user);

    /**
     * 查询角色对应的用户
     *
     * @param roleId 角色id
     * @return list
     */
    @Select("select u.id, u.user_account " +
            "from system_user_role ur, " +
            "     system_user u " +
            "where ur.user_id = u.id " +
            "  and ur.role_id = #{roleId} " +
            "  and u.user_status = 'NORMAL'")
    List<SystemUser> queryUsers(Long roleId);
}
